package com.gykj.rtm.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.gykj.admin.common.constants.RespResultConstants;
import com.gykj.admin.common.entity.User;
import com.gykj.admin.common.entity.UserRole;
import com.gykj.admin.common.resp.RespResultJson;
import com.gykj.rtm.facade.admin.UserFacade;

/**
 * Created by rohto.li on 2017/11/03.
 */
@Api(value = "用户操作接口")
@RestController
@RequestMapping("/users")
public class UserControllerFacade {
	
	@Autowired
	UserFacade userFacade;
	
	@ApiOperation(value = "检索用户" ,  notes="检索用户")
    @RequestMapping(value = "/getAllUserList", method = RequestMethod.POST)
    public RespResultJson getAllUserList (
    		User user,
            @RequestParam(value="currentPage",required = false, defaultValue = "1") int currentPage,
            @RequestParam(value="pageSize",required = false, defaultValue = "20") int pageSize,
            @RequestParam(value="sortColumn",required = false, defaultValue = "1") int sortColumn,
            @RequestParam(value="sortDesc",required = false, defaultValue = "1") int sortDesc
            )
    {
    	RespResultJson result = new RespResultJson();
    	Map<String,Object> map = new HashMap<>();
    	try{
    		PageInfo<User> pageInfo = userFacade.getAllUserList(user, currentPage, pageSize, sortColumn, sortDesc);
    		map.put("rows", pageInfo.getList());
    		map.put("total", pageInfo.getTotal());
    		
        	result.setStatus(true);
        	result.setResult(map);
    	}catch(Exception e) {
        	result.setStatus(false);
    		result.setMessage(e.getMessage());
    	}
        return result;
    }
	
	@ApiOperation(value = "检索用户" ,  notes="通过用户ID来检索用户")
    @RequestMapping(value = "/getUserById", method = RequestMethod.GET)
    public RespResultJson getUserById(@RequestParam(value = "uid",required = false) Integer uid)
    {
    	RespResultJson result = new RespResultJson();
    	Map<String,Object> map = new HashMap<>();
    	try{
    		List<User> list = userFacade.getUserById(uid);
    		map.put("rows", list);
    		map.put("total", 1);
    		
        	result.setStatus(true);
        	result.setResult(map);
    	}catch(Exception e) {
        	result.setStatus(false);
    		result.setMessage(e.getMessage());
    	}
        return result;
    }
    
    /**
     * 保存用户角色
     * @param userRole 用户角色
     *  	  此处获取的参数的角色id是以 “,” 分隔的字符串
     * @return
     */
	@ApiOperation(value = "保存用户角色" ,  notes="保存用户角色")
    @RequestMapping(value = "/saveUserRoles", method = RequestMethod.POST)
    public RespResultJson saveUserRoles(@RequestBody UserRole userRole) {
    	RespResultJson result = new RespResultJson();
    	//1. 校验页面传参的合法性
        if (StringUtils.isEmpty(userRole.getUserId())) {
        	result.setStatus(RespResultConstants.F);
        	result.setMessage(" 参数为空！");
        }
        
        try {
        	//2.调接口添加添加角色，接口已经封装了返回消息。
        	result = userFacade.saveUserRoles(userRole);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(RespResultConstants.F);
            result.setMessage(e.getMessage());
        }
        return result;
    }
	
	@ApiOperation(value = "保存用户" ,  notes="保存用户")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public RespResultJson add(@RequestBody User user) {
    	RespResultJson result = new RespResultJson();
        try {
        	//调用接口添加用户，返回信息已经做了封装。
        	result = userFacade.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
			 result.setStatus(false);
			 result.setMessage("用户: "+user.getUserNameEg()+"添加时出错，原因："+e.getMessage());
        }
        return result;
    }

	@ApiOperation(value = "用户删除" ,  notes="通过用户ID删除用户")
/*	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户ID", required = true ,dataType = "Integer")
  })*/
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public RespResultJson delete(@RequestParam(value = "id",required = false) Integer id) {
		RespResultJson result = new RespResultJson();
		//校验参数
    	if (id == null || id == 0) {
			 result.setStatus(false);
			 result.setMessage("参数为空！");
			 return result;
    	}
    	//调用接口删除用户,接口已经封装了返回消息。
        try {
        	result = userFacade.deleteUser(id);
        } catch (Exception e) {
            e.printStackTrace();
			 result.setStatus(false);
			 result.setMessage("删除失败，原因："+e.getMessage());
        }
        return result;
    }
}
